package com.live.dengdengtv.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.bean.gson.TopicListItem;
import com.live.dengdengtv.bean.gson.TopicRecommend;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.utils.DpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by leiyun on 2016/8/4.
 */
public class SearchActivity extends BaseActivity implements View.OnClickListener{

    private List<String> topicList = new ArrayList<String>();
    private BaseAdapter adapter;

    GridView gridview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        gridview = (GridView)findViewById(R.id.gridview);

        adapter = new GridAdapter(getApplicationContext(),topicList);
        gridview.setAdapter(adapter);

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==5){
                    //打开热门话题页面
                    Intent intent = new Intent(context, HotTopicActivity.class);
                    startActivity(intent);
                }else{
                    //
                }
            }
        });

        getRecommend();
    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.back_ib){
            finish();
        }
    }


    private void getRecommend(){
        HashMap<String,String> params = new HashMap<>();
        params.put("limit","5");
        params.putAll(AppInfo.getHeadInfo());
        requestData(NetTaskList.search_recommend, params, 1);
    }

    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if(response.result == 0){
            if (id == 1) {
                topicList.clear();
                JsonArray array = response.data.getAsJsonArray();
                for (int i=0; i<array.size() ;i++){
                    TopicRecommend liveItem = new Gson().fromJson(array.get(i).getAsJsonObject(), TopicRecommend.class);
                    topicList.add("#"+liveItem.content+"#");
                }
                topicList.add("热门话题>");
                adapter.notifyDataSetChanged();
            }
        }
        return false;
    }



    private class GridAdapter extends BaseAdapter {

        private Context context;
        private List<String> texts;

        public GridAdapter(Context context, List<String> texts){
            this.context = context;
            this.texts = texts;
        }

        @Override
        public int getCount() {
            return texts.size();
        }

        @Override
        public Object getItem(int position) {
            return texts.size();
        }

        @Override
        public long getItemId(int position) {
            return texts.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv;
            if(convertView==null){
                tv = new TextView(context);
                tv.setGravity(Gravity.CENTER);
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);//12sp
                tv.setTextColor(0XFF999999);//#999999
                tv.setBackgroundColor(0xFFfcf8f9);
                AbsListView.LayoutParams param = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DpUtil.dip2px(context,50));//40dp
                tv.setLayoutParams(param);

            }else{
                tv = (TextView)convertView;
            }
            tv.setText(texts.get(position));
            return tv;
        }
    }
}
